import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:todo/model/task.dart';
import 'package:todo/utils/toast.dart';
import 'package:todo/service/task_dao.dart';

class AddTaskPage extends StatefulWidget {
  @override
  _AddTaskPageState createState() => _AddTaskPageState();
}

class _AddTaskPageState extends State<AddTaskPage> {
  TaskDao taskDao;
  Task addTask;
  TextEditingController contentController = TextEditingController();

  @override
  void initState() {
    super.initState();
    taskDao = TaskDao();
    addTask = Task(
      endTime: DateTime.now(),
      isFinish: false,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("添加任务"),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add_circle_outline),
            onPressed: (){
              addTask.content = contentController.text;
              taskDao.insertTask(addTask);
              Toast.toast(context,msg: "添加成功成功！",position: ToastPostion.top);
              Navigator.of(context).pop();
            },
          ),
          SizedBox(width: 5,)
        ],
      ),
      body: Container(
        margin: EdgeInsets.all(6),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 8,),
            _buildContent(),
            SizedBox(height: 8,),
            _buildEndTime(context),
            Divider(height:10.0,indent:0.0,color: Colors.grey[600]),
            _buildIsFinsh(),
          ],
        ),
      ),
    );
  }

  Widget _buildContent(){
    return TextField(
      controller: contentController,
      keyboardType: TextInputType.multiline,
      maxLines: 10,
      style: TextStyle(fontSize: 20),
      decoration: InputDecoration(
        labelText: "内容",
        border: OutlineInputBorder(),
      ),
    );
  }

  Widget _buildEndTime(BuildContext context){
    return GestureDetector(
      onTap: (){
        showDatePicker(
          locale: Locale('zh'),
          context: context,
          initialDate: addTask.endTime,
          firstDate: DateTime(2012),
          lastDate: DateTime(2050),
        ).then((value) {
          if(value!=null){
            setState(() {
              addTask.endTime = value;
            });
          }
        });
      },
      child: Row(
        children: <Widget>[
          Text(
            " 截止时间:  ${addTask.endTimeStr}   ",
            style: TextStyle(
              fontSize: 20,
            ),
          ),
          Icon(Icons.date_range,color: Colors.blue[500],)
        ],
      ),
    );
  }

  Widget _buildIsFinsh(){
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Text(" 是否已完成",style: TextStyle(fontSize: 18),),
        Switch(
          value: addTask.isFinish,//当前状态
          onChanged:(value){
            //重新构建页面
            setState(() {
              addTask.isFinish=value;
            });
          },
        ),
      ],
    );
  }
}
